package com.xjrsoft.common.dbmodel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * SqlPara
 * 封装查询使用的 sql 与参数，主要用于 getSqlPara(...) 返回值
 */
public class SqlParam implements Serializable {

    private static final long serialVersionUID = -8586448059592782381L;

    String sql;
    List<Object> paraList;

    public SqlParam setSql(String sql) {
        this.sql = sql;
        return this;
    }

    public SqlParam addPara(Object para) {
        if (paraList == null) {
            paraList = new ArrayList<Object>();
        }
        paraList.add(para);
        return this;
    }

    public SqlParam addParaList(List<Object> paraList) {
        if (this.paraList == null) {
            this.paraList = paraList;
        } else {
            this.paraList.addAll(paraList);
        }
        return this;
    }

    public String getSql() {
        return sql;
    }

    public Object[] getPara() {
        if (paraList == null || paraList.size() == 0) {
            return new Object[0];
        } else {
            return paraList.toArray(new Object[paraList.size()]);
        }
    }

    public List<Object> getParaList() {
        if (this.paraList == null) {
            this.paraList = new ArrayList<>();
        }
        return this.paraList;
    }

    public SqlParam clear() {
        sql = null;
        if (paraList != null) {
            paraList.clear();
        }
        return this;
    }

    public String toString() {
        return "Sql: " + sql + "\nPara: " + paraList;
    }
}
